﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EzDesk.Apps.ServiceImpl.Attendance.Repositories.Interface;
using EzDesk.Apps.Model.Attendance;

namespace EzDesk.Apps.ServiceImpl.Attendance.Repositories
{
    public partial class LeaveInfoRepository : ILeaveInfoRepository
    {
        EzDeskAttendanceDataContext _db;

        public LeaveInfoRepository(EzDeskAttendanceDataContext db)
        {
            this._db = db;
        }

        public Guid Add(LeaveInfo model, bool saveChanges)
        {
            _db.ez_LeaveInfos.InsertOnSubmit(
                new ez_LeaveInfo()
                {
                    LeaveId = model.Id,
                    UserId = model.UserId,
                    LeaveTypeId = model.LeaveTypeId,
                    LeaveTypeName = model.LeaveTypeName,
                    StartTime = model.StartTime,
                    EndTime = model.EndTime,
                    Description = model.Description,
                    IsForWork = model.IsForWork,
                    IsAgreed = model.IsAgreed,

                    IsAudited = model.IsAudited,
                    AuditNote = model.AuditNote,
                    AuditTime = model.AuditTime,
                    AuditUserId = model.AuditUserId,

                    CreateTime = model.CreateTime,
                });
            if (saveChanges)
            {
                this.SaveChanges();
            }
            return model.Id;
        }

        public LeaveInfo GetById(Guid id)
        {
            LeaveInfo info = null;
            if (id != null)
            {
                info = _db.ez_LeaveInfos.Where(p => p.LeaveId == id)
                    .Select(p => new LeaveInfo()
                    {
                        Id = p.LeaveId,
                        UserId = p.UserId,
                        StartTime = p.StartTime,
                        EndTime = p.EndTime,
                        Description = p.Description,
                        IsForWork = p.IsForWork,
                        LeaveTypeId = p.LeaveTypeId,
                        LeaveTypeName = p.LeaveTypeName,

                        IsAudited = p.IsAudited,
                        AuditNote = p.AuditNote,
                        AuditUserId = p.AuditUserId.GetValueOrDefault(),
                        AuditTime = p.AuditTime.GetValueOrDefault(DateTime.Now),                        
                        IsAgreed = p.IsAgreed.GetValueOrDefault(),

                        CreateTime = p.CreateTime,
                    })
                    .SingleOrDefault();
            }
            return info;
        }

        public IQueryable<LeaveInfo> GetAll()
        {
            return _db.ez_LeaveInfos.Select(
                p => new LeaveInfo()
                {
                    Id = p.LeaveId,
                    UserId = p.UserId,
                    StartTime = p.StartTime,
                    EndTime = p.EndTime,
                    Description = p.Description,
                    IsForWork = p.IsForWork,
                    LeaveTypeId = p.LeaveTypeId,
                    LeaveTypeName = p.LeaveTypeName,

                    IsAudited = p.IsAudited,
                    AuditNote = p.AuditNote,
                    AuditUserId = p.AuditUserId.GetValueOrDefault(),
                    AuditTime = p.AuditTime.GetValueOrDefault(DateTime.Now),
                    IsAgreed = p.IsAgreed.GetValueOrDefault(),

                    CreateTime = p.CreateTime,
                }
                );
        }

        public void Delete(Guid id, bool saveChanges)
        {
            ez_LeaveInfo entity = _db.ez_LeaveInfos.Where(p => p.LeaveId == id).SingleOrDefault();
            if (entity != null)
            {
                _db.ez_LeaveInfos.DeleteOnSubmit(entity);
            }
            if (saveChanges)
            {
                SaveChanges();
            }
        }

        public void Update(LeaveInfo model, bool saveChanges)
        {
            var entity = _db.ez_LeaveInfos.Where(p => p.LeaveId == model.Id)
                .SingleOrDefault();

            entity.UserId = model.UserId;
            entity.StartTime = model.StartTime;
            entity.EndTime = model.EndTime;
            entity.Description = model.Description;
            entity.IsForWork = model.IsForWork;
            entity.LeaveTypeId = model.LeaveTypeId;
            entity.LeaveTypeName = model.LeaveTypeName;

            entity.IsAudited = model.IsAudited;
            entity.AuditNote = model.AuditNote;
            entity.AuditUserId = model.AuditUserId;
            entity.AuditTime = model.AuditTime;
            entity.IsAgreed = model.IsAgreed;

            if (saveChanges)
            {
                SaveChanges();
            }
        }

        public void SaveChanges()
        {
            _db.SubmitChanges();
        }

        /// <summary>
        /// 按照时间，用户，是否审核 得到LeaveinfoList
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <param name="userId"></param>
        /// <param name="isAudited"></param>
        /// <returns></returns>
        public List<LeaveInfo> GetLeaveInfoList(
            DateTime? startTime, DateTime? endTime, 
            DateTime? createTimeStart,
            DateTime? createTimeEnd,
            Guid? userId, 
            bool? isAudited = null, 
            bool? isAgreed = null
            )
        {
            IQueryable<ez_LeaveInfo> query = _db.ez_LeaveInfos;

            if (startTime.HasValue)
            {
                query = query.Where(p => p.StartTime >= startTime);
            }

            if (endTime.HasValue)
            {
                query = query.Where(p => p.EndTime <= endTime);
            }

            if (createTimeStart.HasValue)
            {
                query = query.Where(p => p.CreateTime >= createTimeStart);
            }

            if (createTimeEnd.HasValue)
            {
                query = query.Where(p => p.CreateTime <= createTimeEnd);
            }

            if (userId.HasValue)
            {
                query = query.Where(p => p.UserId == userId.GetValueOrDefault());
            }

            if (isAudited.HasValue)
            {
                query = query.Where(p => p.IsAudited == isAudited.GetValueOrDefault());

                if (isAgreed.HasValue)
                {
                    query = query.Where(p => p.IsAgreed == isAgreed.GetValueOrDefault());
                }
            }



            return query.Select(p=> new LeaveInfo(){
                Id = p.LeaveId,
                LeaveTypeName = p.LeaveTypeName,
                LeaveTypeId = p.LeaveTypeId,
                StartTime = p.StartTime,
                EndTime = p.EndTime,
                UserId = p.UserId,
                IsForWork = p.IsForWork,
                IsAgreed = p.IsAgreed.GetValueOrDefault(),
                Description = p.Description,
                IsAudited = p.IsAudited,
                AuditNote = p.AuditNote,
                AuditTime = p.AuditTime.GetValueOrDefault(DateTime.Now),
                AuditUserId = p.AuditUserId.GetValueOrDefault(),
                CreateTime = p.CreateTime,
            }).ToList();
        }
    }
}
